package day16;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;

/**
 * @author Xiao
 * @date 2021/8/10
 */
public class Test05 {
    /**
     * 5. 需求：
     *     1. 创建ArrayList<String> list = new ArrayList<>();对象，添加三个字符串
     *     2. 利用反射技术给list集合添加几个int类型的整数。
     */
    public static void main(String[] args) throws NoSuchMethodException,
            InvocationTargetException, IllegalAccessException {
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list, "阿良", "平安", "左右");
        Class<? extends ArrayList> clazz = list.getClass();
        Method add = clazz.getMethod("add", Object.class);
        add.invoke(list, 10);
        add.invoke(list, 20);
        add.invoke(list, 30);
        System.out.println(list);
        System.out.println("----------------");
        Method get = clazz.getMethod("get", int.class);
        for (int i = 0; i < list.size(); i++) {
            System.out.print(get.invoke(list, i) + "\t");
        }
        System.out.println();
    }
}
